<?php
declare(strict_types=1);

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Logic\Menu as MenuLogic;
use App\Logic\Role as RoleLogic;
use App\Logic\Account as AccountLogic;
use Illuminate\Support\Facades\DB;

class IndexController extends Controller
{
    private $menuLogic = null;
    private $roleLogic = null;
    private $accountLogic = null;

    public function __construct()
    {
        $this->menuLogic = new MenuLogic();
        $this->roleLogic = new RoleLogic();
        $this->accountLogic = new AccountLogic();
    }

    public function index()
    {
        return view('index.index');
    }

    public function console(Request $request)
    {
        $count = [

        ];
        $sysInfo = [
            'os' => PHP_OS,
            'space' => round((disk_free_space('.')/(1024*1024)),2).'M',
            'addr' => $_SERVER['HTTP_HOST'],
            'run' => $request->server('SERVER_SOFTWARE'),
            'php' => PHP_VERSION,
            'php_run' => php_sapi_name(),
            'laravel' => app()->version(),
            'upload' => ini_get('upload_max_filesize'),
            'max' => ini_get('max_execution_time').'秒',
        ];
        return view('index.console', ['sysInfo' => $sysInfo, 'count' => $count]);
    }

    public function menu(Request $request)
    {
       $accountId = $request->session()->get('account_id');
       $topMenu = $this->menuLogic->getMenu();
       $result = array();
       if ($accountId == 1) {
            foreach ($topMenu as $v) {
                $v['title'] = $v['menu_name'];
                $v['icon'] = 'layui-icon' . $v['menu_icon'];
                $v['type'] = 0;
                if ($this->menuLogic->countMenu((int)$v['id']) > 0) {
                    $subMenu = $this->menuLogic->getMenu((int)$v['id']);
                    for ($i=0; $i < count($subMenu); $i++) {
                        $v['children'][$i]['id'] = $subMenu[$i]['id'];
                        $v['children'][$i]['title'] = $subMenu[$i]['menu_name'];
                        $v['children'][$i]['icon'] = "layui-icon ".$subMenu[$i]['menu_icon'];
                        $v['children'][$i]['type'] = 1;
                        $v['children'][$i]['openType'] = "_iframe";
                        $v['children'][$i]['href'] = $subMenu[$i]['href'];
                    }
                } else {
                    $v['children'] = [];
                }
                $result[] = $v;
            }
       } else {
            $roleId = $this->accountLogic->getRoleIdByAccountId((int)$accountId);
            $menuIds = $this->roleLogic->findMenuIdsByRoleId((int)$roleId);
            foreach ($topMenu as $v) {
                $v['title'] = $v['menu_name'];
                $v['icon'] = 'layui-icon' . $v['menu_icon'];
                $v['type'] = 0;
                if ($this->menuLogic->countMenu((int)$v['id']) > 0) {
                    $subMenu = $this->menuLogic->getMenu((int)$v['id']);
                    for ($i=0; $i < count($subMenu); $i++) {
                        if (in_array($subMenu[$i]['id'], $menuIds)) {
                            $v['children'][$i]['id'] = $subMenu[$i]['id'];
                            $v['children'][$i]['title'] = $subMenu[$i]['menu_name'];
                            $v['children'][$i]['icon'] = "layui-icon ".$subMenu[$i]['menu_icon'];
                            $v['children'][$i]['type'] = 1;
                            $v['children'][$i]['openType'] = "_iframe";
                            $v['children'][$i]['href'] = $subMenu[$i]['href'];
                        }
                    }
                } else {
                    $v['children'] = [];
                }
                $result[] = $v;
            }

       }
       return response()->json($result);
    }
}
